<!DOCTYPE html>
<html lang="cn-zh">

<head>
  
  <meta charset="utf-8">



<meta name="viewport" content="width=device-width, initial-scale=1.0">


  
    <meta name="description" content=" 需求分析 进入界面开启蓝牙连接 中间有跳转其他页面，需要再其他页面保持蓝牙连接状态 退出界面后需要断开蓝牙连接 解决办法 在viewDidDisappear中处理连接断开问题
在该方法中，如果是跳转界面self.navigationController不为空，如果退出当前界面得到的值为nil
override func viewDidDisappear(_ animated: Bool) { if self.navigationController == nil { // todo 调用蓝牙断开方法 } } ">
  


<meta name="color-scheme" content="light dark">







<meta name="generator" content="Hugo 0.111.2">

<meta content="" name="keywords">
<meta content="iOS navigationController 跳转页面不销毁对象，退出销毁对象 - 通通不同-博客" property="og:title">
<meta content="开发的乐趣在于相通而不同 - " property="og:description">

<meta name="msvalidate.01" content="F56ED94B7E86941E3DACEF208F07D329" />

  <title>
    
    iOS navigationController 跳转页面不销毁对象，退出销毁对象
     - 通通不同-博客
    
</title>

  <link rel="canonical" href="https://codebeautiful.gitee.io/post/ios_viewdiddisappear_without_destroying_objects/">


  








  
    
  
  
  <link rel="stylesheet" href="/css/base.min.4f231885947d13c748c815d93da49f07ab89afb67629f9970b7c054610e88e0b.css" integrity="sha256-TyMYhZR9E8dIyBXZPaSfB6uJr7Z2KfmXC3wFRhDojgs=" crossorigin="anonymous">



</head>

<body>
  <nav class="u-background">
  <div class="u-wrapper">
    <ul class="Banner">
      <li class="Banner-item Banner-item--title">
        <h1 class="Banner-heading">
          <a class="Banner-link u-clickable" href="/">通通不同-博客</a>
        </h1>
      </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/post/">全部</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/categories/">分类</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/tags/">标签</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/about/">关于</a>
        </li>
      
    </ul>
  </div>
</nav>

  <main>
    <div class="u-wrapper">
      <div class="u-padding">
        

  <article>
    <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/ios_viewdiddisappear_without_destroying_objects/" rel="bookmark">iOS navigationController 跳转页面不销毁对象，退出销毁对象</a>
  </h2>
  
    <time datetime="2023-04-21T11:49:45&#43;08:00">21 April, 2023</time>
  
</header>

    <h3 id="需求分析">
  <a class="Heading-link u-clickable" href="/post/ios_viewdiddisappear_without_destroying_objects/#%e9%9c%80%e6%b1%82%e5%88%86%e6%9e%90">需求分析</a>
</h3>
<ol>
<li>进入界面开启蓝牙连接</li>
<li>中间有跳转其他页面，需要再其他页面保持蓝牙连接状态</li>
<li>退出界面后需要断开蓝牙连接</li>
</ol>
<h3 id="解决办法">
  <a class="Heading-link u-clickable" href="/post/ios_viewdiddisappear_without_destroying_objects/#%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95">解决办法</a>
</h3>
<p>在viewDidDisappear中处理连接断开问题</p>
<p>在该方法中，如果是跳转界面self.navigationController不为空，如果退出当前界面得到的值为nil</p>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-swift" data-lang="swift"><span style="display:flex;"><span><span style="color:#66d9ef">override</span> <span style="color:#66d9ef">func</span> <span style="color:#a6e22e">viewDidDisappear</span>(<span style="color:#66d9ef">_</span> animated: Bool) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">self</span>.navigationController == <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>    		<span style="color:#75715e">// todo 调用蓝牙断开方法</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>

    


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/ios/" rel="tag">iOS</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/uinavigationcontroller/" rel="tag">UINavigationController</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E5%AF%B9%E8%B1%A1%E9%94%80%E6%AF%81/" rel="tag">对象销毁</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E5%AF%B9%E8%B1%A1%E4%BF%9D%E7%95%99/" rel="tag">对象保留</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E8%93%9D%E7%89%99/" rel="tag">蓝牙</a>
            </li>
          
        </ul>
      
    
  </footer>

    
  

  </article>


      </div>
    </div>
  </main>
  
  <footer class="Footer">
    <div class="u-wrapper">
      <div class="u-padding">
        开发的乐趣在于相通而不同
      </div>
    </div>
  </footer>


  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d08a2ad355cb7a065a41be081b43297e";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>

</body>

</html>
